#include "managerop.h"
#include "ui_managerop.h"
#include <QMessageBox>
#include "myprotocol.h"

ManagerOP::ManagerOP(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ManagerOP)
{
    ui->setupUi(this);
    this->setFixedSize(this->size().width(),this->size().height());
    this->setWindowIcon(QIcon(":/new/prefix1/Image/mesage.png"));
    this->setWindowTitle("ROOT OP");
    QPalette qp = this->palette(); //重新拷贝一份和本对象palette相同的palette
    qp.setBrush(QPalette::Window,QBrush(QImage(":/new/prefix1/Image/Frame.jpg").scaled(this->size())));
    this->setPalette(qp);
    this->ui->pic->setPixmap(QPixmap(":/new/prefix1/Image/sunny.png"));

    this->myregister = new Register(this);
    this->myregister->setWindowFlag(Qt::Window);
    this->myregister->hide();

    this->myusrs = new SelectUsr(this);
    this->myusrs->setWindowFlag(Qt::Window);
    this->myusrs->hide();

    connect(this,&ManagerOP::my_register_signal,this,&ManagerOP::my_register_slot);
    connect(this,&ManagerOP::my_delete_signal,this,&ManagerOP::my_delete_slot);
    connect(this,&ManagerOP::my_select_allusrs_signal,&ManagerOP::my_select_allusrs_slot);

    connect(this,&ManagerOP::my_clear_text_signal,this,&ManagerOP::my_clear_text_slot);

    //返回信号
    connect(this->myregister,&Register::my_back_to_managerop_signal,[=](){
        this->show();
    });
    connect(this->myusrs,&SelectUsr::my_back_to_managerop_signal,[=](){
        this->show();
    });

    this->ui->query_btn->setShortcut(Qt::Key_Return);
}

ManagerOP::~ManagerOP()
{
    delete ui;
}

void ManagerOP::set_socket(QTcpSocket *socket)
{
    this->socket = socket;
}

void ManagerOP::on_register_btn_clicked()
{
    this->myregister->set_socket(socket);
    this->hide();
    this->myregister->show();

    this->ui->staff_line->clear();
}

void ManagerOP::my_register_slot(bool if_ok)
{
    if(if_ok)
    {
        QMessageBox::information(this->myregister,"successful","register successful");
    }
    else
    {
        QString error_str = "register error";
        QMessageBox::critical(this->myregister,"error",error_str);
    }
}

void ManagerOP::my_delete_slot(bool if_ok)
{
    if(if_ok)
    {
        QMessageBox::information(this->myregister,"successful","delete successful");
    }
    else
    {
        QString error_str = "delete error";
        QMessageBox::critical(this->myregister,"error",error_str);
    }
}

void ManagerOP::on_add_btn_clicked()
{
    emit this->my_add_signal();

    this->ui->staff_line->clear();
}

void ManagerOP::on_selectstaff_btn_clicked()
{
    emit this->my_select_staff_signal();

    this->ui->staff_line->clear();
}

void ManagerOP::on_query_btn_clicked()
{
    if(this->ui->staff_line->text().isEmpty())
    {
        QString error_str = "staff name empty!";
        QMessageBox::critical(this, "error", error_str);
        return;
    }
    char send_info[512] = {0};
    sprintf(send_info,"%d",managerquery_request_protocol);
    trans_dbinfo_t myinfo;
    QByteArray name = this->ui->staff_line->text().toUtf8();
    strcpy(myinfo.name,name.data());
    memcpy(send_info + 2,&myinfo,sizeof(myinfo));

    this->socket->write(send_info,sizeof(send_info));
}

void ManagerOP::on_selectusr_btn_clicked()
{
    QString mydata = QString::number(select_allusr_request_protocol);
    mydata += "no message";
    this->socket->write(mydata.toUtf8());

    this->ui->staff_line->clear();
}

void ManagerOP::my_select_allusrs_slot(QStringList strlist)
{
    this->myusrs->show();
    emit this->myusrs->my_select_signal(strlist);
}

void ManagerOP::my_clear_text_slot()
{
    this->ui->staff_line->clear();
}

void ManagerOP::on_back_btn_clicked()
{
    emit this->my_back_to_widget_signal();
    this->hide();
}
